<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Events;

use \GatewayWorker\Lib\Gateway;

/**
 * Description of onMessage
 *
 * @author saisai
 */
class onMessage
{

    private static $arrayType = [
        'Login', //登陆
        'User', //用户
        'Msg', //消息
        'Admin', //管理
        'Group', //房间
        'Ping', //心跳
    ];
    private static $typeFunc  = [
        'Login' => [
            'login', 'logout'
        ],
        'User'  => [
            'info', 'friends'
        ],
        'Msg'   => [
            'toUser', 'toAll'
        ],
    ];

    public static function run($client_id, $message)
    {

        $data = objarray_to_array(json_decode($message));

        self::insertlog($data);

        //消息 已经接受 回执
        $re_msg           = \Controller\Receipt::been_received($client_id, $data);
        Gateway::sendToCurrentClient($re_msg);
        $data['now_time'] = time();
        $data['type']     = ucfirst($data['type']);
        //消息处理

        if (in_array($data['type'], self::$arrayType)) {
            //有处理消息的控制器
            self::allot($client_id, $data);
        } else {
            //没有处理消息的控制器  回复给 发送人 (当前客户端) 消息无法处理
            Gateway::sendToCurrentClient(\Data\Massage::msg('消息无法处理!', $client_id, $data));
        }

        unset($re_msg);
        unset($data);
        unset($client_id);
        unset($message);
    }

    /**
     * 插入消息初始化  日志
     * @param type $data
     */
    private static function insertlog($data)
    {
        $info = [
            'REQUEST_TIME' => $_SERVER['REQUEST_TIME'],
            'data'         => $data,
        ];

        $data2 = [
            'type'        => 'start',
            'content'     => print_r($info, true),
            'create_time' => $_SERVER['REQUEST_TIME'],
            'runtype'     => $data['type'],
            'runaction'   => $data['action']
        ];
        \think\Db::name('log')->insert($data2);
    }

    /**
     * 分配控制器!
     * @param type $client_id
     * @param type $data
     */
    static function allot($client_id, $data)
    {
        $Controller = '\\Controller\\' . $data['type'];
        $action     = $data['action'];
        if ($data['type'] != 'Ping') {
            dump($Controller, $action);
        }

        //存在消息类型
        //判断是否存在消息动作
        if (isset(self::$typeFunc[$data['type']])) {
            //有可选动作
            if (in_array($data['action'], self::$typeFunc[$data['type']])) {
                //存在可执行动作
                pr("存在可执行动作 ");
                $Controller::$action($client_id, $data);
            } else {
                //不存在可执行动作 执行run
                pr("不存在可执行动作 执行run");
                $Controller::run($client_id, $data);
            }
        } else {
            pr("没有可选动作 执行run ");
            //没有可选动作 执行run
            $Controller::run($client_id, $data);
        }
    }

    //put your code here
}
